#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int minSwapsCouples(vector<int>& row) {
        int cnt = 0;
        vector<int> val2idx(row.size());
        for (int i = 0; i < row.size(); i++) {
            val2idx[row[i]] = i;
        }
        for (int i = 0; i < row.size() - 1; i += 2) {
            if (row[i + 1] != (row[i] ^ 1)) {
                int src = i + 1, target = val2idx[row[i] ^ 1];
                swap(val2idx[row[src]], val2idx[row[target]]);
                swap(row[src], row[target]);
                cnt++;
            }
        }
        return cnt;
    }
};

//int main()
//{
//    return 0;
//}